package com.smart.translator.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import com.smart.translator.R;
import com.smart.translator.view.DraggableCircleView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KeySettingFloatService extends Service {
    private WindowManager windowManager;
    private List<DraggableCircleView> circleViews = new ArrayList<>();
    private View controlPanel;
    private static final String PREFS_NAME = "key_settings";
    private static final String KEY_POSITIONS = "key_positions";
    private static final String KEY_CONTROL_PANEL_X = "control_panel_x";
    private static final String KEY_CONTROL_PANEL_Y = "control_panel_y";
    private int currentNumber = 1;
    private boolean globalEditMode = false; // 全局编辑模式标志
    private int controlPanelLeft, controlPanelTop, controlPanelRight, controlPanelBottom;

    @Override
    public void onCreate() {
        super.onCreate();
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        setupControlPanel();
        loadSavedPositions();
    }

    private void setupControlPanel() {
        controlPanel = LayoutInflater.from(this).inflate(R.layout.layout_key_control_panel, null);
        ImageButton addButton = controlPanel.findViewById(R.id.btn_add);
        ImageButton saveButton = controlPanel.findViewById(R.id.btn_save);
        ImageButton zoomInButton = controlPanel.findViewById(R.id.btn_zoom_in);
        ImageButton zoomOutButton = controlPanel.findViewById(R.id.btn_zoom_out);

        int type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                WindowManager.LayoutParams.TYPE_PHONE;

        WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                type,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT
        );
        windowParams.gravity = Gravity.LEFT | Gravity.TOP;
        
        // 从保存的位置加载控制面板位置，如果没有保存则使用默认位置
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        int savedX = prefs.getInt(KEY_CONTROL_PANEL_X, -1);
        int savedY = prefs.getInt(KEY_CONTROL_PANEL_Y, -1);
        
        if (savedX != -1 && savedY != -1) {
            windowParams.x = savedX;
            windowParams.y = savedY;
        } else {
            windowParams.x = getResources().getDisplayMetrics().widthPixels - 200; // 初始位置在右侧
            windowParams.y = getResources().getDisplayMetrics().heightPixels / 2 - 100; // 垂直居中
        }

        // 设置控制面板的触摸监听器，支持拖动
        final float[] lastTouchX = {0};
        final float[] lastTouchY = {0};
        final int[] initialX = {windowParams.x};
        final int[] initialY = {windowParams.y};
        final boolean[] isDragging = {false};

        controlPanel.setOnTouchListener((v, event) -> {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    lastTouchX[0] = event.getRawX();
                    lastTouchY[0] = event.getRawY();
                    initialX[0] = windowParams.x;
                    initialY[0] = windowParams.y;
                    isDragging[0] = false;
                    android.util.Log.d("KeySettingFloat", "控制面板 ACTION_DOWN");
                    return true;

                case MotionEvent.ACTION_MOVE:
                    float dx = event.getRawX() - lastTouchX[0];
                    float dy = event.getRawY() - lastTouchY[0];
                    
                    if (Math.abs(dx) > 10 || Math.abs(dy) > 10) {
                        isDragging[0] = true;
                    }
                    
                    if (isDragging[0]) {
                        windowParams.x = initialX[0] + (int) dx;
                        windowParams.y = initialY[0] + (int) dy;
                        
                        try {
                            windowManager.updateViewLayout(controlPanel, windowParams);
                            // 更新控制面板位置信息
                            updateControlPanelPosition();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        }
                    }
                    return true;

                case MotionEvent.ACTION_UP:
                    android.util.Log.d("KeySettingFloat", "控制面板 ACTION_UP, 是否拖动: " + isDragging[0]);
                    if (isDragging[0]) {
                        // 保存控制面板位置
                        prefs.edit()
                            .putInt(KEY_CONTROL_PANEL_X, windowParams.x)
                            .putInt(KEY_CONTROL_PANEL_Y, windowParams.y)
                            .apply();
                        android.util.Log.d("KeySettingFloat", "保存控制面板位置: x=" + windowParams.x + ", y=" + windowParams.y);
                    }
                    if (!isDragging[0]) {
                        // 如果没有拖动，则认为是点击事件，让子视图处理
                        return false;
                    }
                    return true;
            }
            return false;
        });

        // 保存控制面板的位置信息，用于删除区域判断
        controlPanel.post(() -> {
            updateControlPanelPosition();
        });

        addButton.setOnClickListener(v -> addNewCircle());
        saveButton.setOnClickListener(v -> {
            savePositions();
            stopSelf();
        });

        // 设置放大缩小按钮的点击事件
        zoomInButton.setOnClickListener(v -> {
            // 只在全局编辑模式下才能放大
            if (globalEditMode) {
                // 只放大处于编辑模式的圆形按钮
                for (DraggableCircleView circle : circleViews) {
                    if (circle.isInDeleteMode()) {
                        float newRadius = circle.getRadius() + 10;
                        if (newRadius <= 100) {
                            circle.setRadius(newRadius);
                            // 更新WindowManager的布局参数
                            WindowManager.LayoutParams circleParams = (WindowManager.LayoutParams) circle.getLayoutParams();
                            if (circleParams != null) {
                                int newSize = (int) (newRadius * 2);
                                circleParams.width = newSize;
                                circleParams.height = newSize;
                                try {
                                    windowManager.updateViewLayout(circle, circleParams);
                                } catch (IllegalArgumentException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        });

        zoomOutButton.setOnClickListener(v -> {
            // 只在全局编辑模式下才能缩小
            if (globalEditMode) {
                // 只缩小处于编辑模式的圆形按钮
                for (DraggableCircleView circle : circleViews) {
                    if (circle.isInDeleteMode()) {
                        float newRadius = circle.getRadius() - 10;
                        if (newRadius >= 30) {
                            circle.setRadius(newRadius);
                            // 更新WindowManager的布局参数
                            WindowManager.LayoutParams circleParams = (WindowManager.LayoutParams) circle.getLayoutParams();
                            if (circleParams != null) {
                                int newSize = (int) (newRadius * 2);
                                circleParams.width = newSize;
                                circleParams.height = newSize;
                                try {
                                    windowManager.updateViewLayout(circle, circleParams);
                                } catch (IllegalArgumentException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        });

        windowManager.addView(controlPanel, windowParams);
    }

    private void addNewCircle() {
        // 创建圆形按钮
        DraggableCircleView circle = new DraggableCircleView(this);
        circle.setNumber(currentNumber++);
        circle.setOnDeleteListener(view -> {
            windowManager.removeView(view);
            circleViews.remove(view);
            reorderButtonNumbers();
        });

        int size = (int) (circle.getRadius() * 2);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                size,
                size,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                        WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT
        );
        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.x = 100;
        params.y = 100;

        // 设置触摸监听
        final float[] lastTouchX = {0};
        final float[] lastTouchY = {0};
        final int[] initialX = {params.x};
        final int[] initialY = {params.y};
        final long[] lastClickTime = {0};
        final boolean[] isEditMode = {false};
        final boolean[] hasMoved = {false};

        circle.setOnTouchListener((v, event) -> {
            android.util.Log.d("KeySettingFloat", "触摸事件: " + event.getActionMasked() + ", 指针数量: " + event.getPointerCount() + ", 原始action: " + event.getAction());
            
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    lastTouchX[0] = event.getRawX();
                    lastTouchY[0] = event.getRawY();
                    initialX[0] = params.x;
                    initialY[0] = params.y;
                    hasMoved[0] = false;
                    android.util.Log.d("KeySettingFloat", "ACTION_DOWN");
                    v.performClick(); // 确保事件被处理
                    return true;

                case MotionEvent.ACTION_MOVE:
                    android.util.Log.d("KeySettingFloat", "ACTION_MOVE: 指针数量=" + event.getPointerCount() + ", 编辑模式=" + isEditMode[0]);
                    
                    // 普通移动
                    float dx = event.getRawX() - lastTouchX[0];
                    float dy = event.getRawY() - lastTouchY[0];
                    
                    if (!isEditMode[0] && (Math.abs(dx) > 10 || Math.abs(dy) > 10)) {
                        hasMoved[0] = true;
                    }
                    
                    params.x = initialX[0] + (int) dx;
                    params.y = initialY[0] + (int) dy;
                    
                    try {
                        windowManager.updateViewLayout(v, params);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                    return true;

                case MotionEvent.ACTION_UP:
                    android.util.Log.d("KeySettingFloat", "ACTION_UP");
                    if (!hasMoved[0]) {
                        // 检测双击
                        long currentTime = System.currentTimeMillis();
                        if (currentTime - lastClickTime[0] < 300) { // 300ms内的双击
                            if (!isEditMode[0]) {
                                // 双击进入编辑模式
                                isEditMode[0] = true;
                                globalEditMode = true; // 设置全局编辑模式
                                circle.showEditMode(true);
                                android.util.Log.d("KeySettingFloat", "双击进入编辑模式");
                            } else {
                                // 双击退出编辑模式
                                isEditMode[0] = false;
                                circle.showEditMode(false);
                                checkAndUpdateGlobalEditMode(); // 检查并更新全局编辑模式
                                android.util.Log.d("KeySettingFloat", "双击退出编辑模式");
                            }
                        } else {
                            // 单击退出编辑模式（如果已经在编辑模式中）
                            if (isEditMode[0]) {
                                isEditMode[0] = false;
                                circle.showEditMode(false);
                                checkAndUpdateGlobalEditMode(); // 检查并更新全局编辑模式
                                android.util.Log.d("KeySettingFloat", "单击退出编辑模式");
                            }
                        }
                        lastClickTime[0] = currentTime;
                    } else if (!isEditMode[0]) {
                        // 检查是否拖到删除栏（只在控制面板区域内有效）
                        // 先更新控制面板位置信息
                        updateControlPanelPosition();
                        
                        int buttonCenterX = params.x + (int) circle.getRadius();
                        int buttonCenterY = params.y + (int) circle.getRadius();
                        
                        // 检查按钮中心是否在控制面板区域内
                        boolean isInControlPanel = buttonCenterX >= controlPanelLeft && 
                                                  buttonCenterX <= controlPanelRight && 
                                                  buttonCenterY >= controlPanelTop && 
                                                  buttonCenterY <= controlPanelBottom;
                        
                        android.util.Log.d("KeySettingFloat", "删除检查: 按钮中心X=" + buttonCenterX + ", Y=" + buttonCenterY + 
                            ", 控制面板区域: left=" + controlPanelLeft + ", top=" + controlPanelTop + 
                            ", right=" + controlPanelRight + ", bottom=" + controlPanelBottom + 
                            ", 是否在控制面板内=" + isInControlPanel);
                        
                        if (isInControlPanel) {
                            // 拖到控制面板区域，删除按钮
                            android.util.Log.d("KeySettingFloat", "删除按钮: " + circle.getNumber());
                            windowManager.removeView(v);
                            circleViews.remove(v);
                            reorderButtonNumbers();
                        }
                    }
                    return true;
            }
            return false;
        });

        windowManager.addView(circle, params);
        circleViews.add(circle);
    }

    private void savePositions() {
        try {
            SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            String existingJson = prefs.getString(KEY_POSITIONS, "[]");
            JSONArray existingArray = new JSONArray(existingJson);
            
            // 创建现有数据的映射，用于保持分组和ID信息
            Map<Integer, JSONObject> existingDataMap = new HashMap<>();
            for (int i = 0; i < existingArray.length(); i++) {
                JSONObject obj = existingArray.getJSONObject(i);
                int number = obj.getInt("number");
                existingDataMap.put(number, obj);
            }
            
            JSONArray array = new JSONArray();
            for (DraggableCircleView circle : circleViews) {
                JSONObject obj = new JSONObject();
                obj.put("number", circle.getNumber());
                
                // 获取WindowManager中的实际位置
                WindowManager.LayoutParams params = (WindowManager.LayoutParams) circle.getLayoutParams();
                if (params != null) {
                    // 保存按钮中心位置
                    float centerX = params.x + circle.getRadius();
                    float centerY = params.y + circle.getRadius();
                    obj.put("x", centerX);
                    obj.put("y", centerY);
                } else {
                    // 如果无法获取参数，使用默认值
                    obj.put("x", 100);
                    obj.put("y", 100);
                }
                
                obj.put("radius", circle.getRadius());
                
                // 检查是否有现有的分组和ID信息，如果有则保持，否则使用默认值
                JSONObject existingData = existingDataMap.get(circle.getNumber());
                if (existingData != null) {
                    obj.put("description", existingData.optString("description", "未设置"));
                    obj.put("groupIndex", existingData.optInt("groupIndex", 0));
                    obj.put("groupId", existingData.optInt("groupId", -1));
                } else {
                    obj.put("description", "未设置");
                    obj.put("groupIndex", 0); // 默认分组：微信
                    obj.put("groupId", -1); // 默认未配置ID
                }
                
                array.put(obj);
            }
            prefs.edit().putString(KEY_POSITIONS, array.toString()).apply();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadSavedPositions() {
        try {
            SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            String json = prefs.getString(KEY_POSITIONS, "[]");
            JSONArray array = new JSONArray(json);
            currentNumber = 1;
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                
                DraggableCircleView circle = new DraggableCircleView(this);
                circle.setNumber(obj.getInt("number"));
                currentNumber = Math.max(currentNumber, obj.getInt("number") + 1);

                // 先设置保存的半径
                float savedRadius = (float) obj.getDouble("radius");
                circle.setRadius(savedRadius);
                
                // 然后根据半径计算大小
                int size = (int) (savedRadius * 2);
                WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        size,
                        size,
                        Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                                WindowManager.LayoutParams.TYPE_PHONE,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                        PixelFormat.TRANSLUCENT
                );
                params.gravity = Gravity.LEFT | Gravity.TOP;
                
                // 将保存的中心位置转换为左上角位置
                float centerX = (float) obj.getDouble("x");
                float centerY = (float) obj.getDouble("y");
                params.x = (int) (centerX - savedRadius);
                params.y = (int) (centerY - savedRadius);

                // 设置触摸监听
                final float[] lastTouchX = {0};
                final float[] lastTouchY = {0};
                final int[] initialX = {params.x};
                final int[] initialY = {params.y};
                final long[] lastClickTime = {0};
                final boolean[] isEditMode = {false};
                final boolean[] hasMoved = {false};

                circle.setOnTouchListener((v, event) -> {
                    android.util.Log.d("KeySettingFloat", "触摸事件: " + event.getActionMasked() + ", 指针数量: " + event.getPointerCount() + ", 原始action: " + event.getAction());
                    
                    switch (event.getActionMasked()) {
                        case MotionEvent.ACTION_DOWN:
                            lastTouchX[0] = event.getRawX();
                            lastTouchY[0] = event.getRawY();
                            initialX[0] = params.x;
                            initialY[0] = params.y;
                            hasMoved[0] = false;
                            android.util.Log.d("KeySettingFloat", "ACTION_DOWN");
                            v.performClick(); // 确保事件被处理
                            return true;

                        case MotionEvent.ACTION_MOVE:
                            android.util.Log.d("KeySettingFloat", "ACTION_MOVE: 指针数量=" + event.getPointerCount() + ", 编辑模式=" + isEditMode[0]);
                            
                            // 普通移动
                            float dx = event.getRawX() - lastTouchX[0];
                            float dy = event.getRawY() - lastTouchY[0];
                            
                            if (!isEditMode[0] && (Math.abs(dx) > 10 || Math.abs(dy) > 10)) {
                                hasMoved[0] = true;
                            }
                            
                            params.x = initialX[0] + (int) dx;
                            params.y = initialY[0] + (int) dy;
                            
                            try {
                                windowManager.updateViewLayout(v, params);
                            } catch (IllegalArgumentException e) {
                                e.printStackTrace();
                            }
                            return true;

                        case MotionEvent.ACTION_UP:
                            android.util.Log.d("KeySettingFloat", "ACTION_UP");
                            if (!hasMoved[0]) {
                                // 检测双击
                                long currentTime = System.currentTimeMillis();
                                if (currentTime - lastClickTime[0] < 300) { // 300ms内的双击
                                    if (!isEditMode[0]) {
                                        // 双击进入编辑模式
                                        isEditMode[0] = true;
                                        globalEditMode = true; // 设置全局编辑模式
                                        circle.showEditMode(true);
                                        android.util.Log.d("KeySettingFloat", "双击进入编辑模式");
                                    } else {
                                        // 双击退出编辑模式
                                        isEditMode[0] = false;
                                        circle.showEditMode(false);
                                        checkAndUpdateGlobalEditMode(); // 检查并更新全局编辑模式
                                        android.util.Log.d("KeySettingFloat", "双击退出编辑模式");
                                    }
                                } else {
                                    // 单击退出编辑模式（如果已经在编辑模式中）
                                    if (isEditMode[0]) {
                                        isEditMode[0] = false;
                                        circle.showEditMode(false);
                                        checkAndUpdateGlobalEditMode(); // 检查并更新全局编辑模式
                                        android.util.Log.d("KeySettingFloat", "单击退出编辑模式");
                                    }
                                }
                                lastClickTime[0] = currentTime;
                            } else if (!isEditMode[0]) {
                                // 检查是否拖到删除栏（只在控制面板区域内有效）
                                // 先更新控制面板位置信息
                                updateControlPanelPosition();
                                
                                int buttonCenterX = params.x + (int) circle.getRadius();
                                int buttonCenterY = params.y + (int) circle.getRadius();
                                
                                // 检查按钮中心是否在控制面板区域内
                                boolean isInControlPanel = buttonCenterX >= controlPanelLeft && 
                                                          buttonCenterX <= controlPanelRight && 
                                                          buttonCenterY >= controlPanelTop && 
                                                          buttonCenterY <= controlPanelBottom;
                                
                                android.util.Log.d("KeySettingFloat", "删除检查: 按钮中心X=" + buttonCenterX + ", Y=" + buttonCenterY + 
                                    ", 控制面板区域: left=" + controlPanelLeft + ", top=" + controlPanelTop + 
                                    ", right=" + controlPanelRight + ", bottom=" + controlPanelBottom + 
                                    ", 是否在控制面板内=" + isInControlPanel);
                                
                                if (isInControlPanel) {
                                    // 拖到控制面板区域，删除按钮
                                    android.util.Log.d("KeySettingFloat", "删除按钮: " + circle.getNumber());
                                    windowManager.removeView(v);
                                    circleViews.remove(v);
                                    reorderButtonNumbers();
                                }
                            }
                            return true;
                    }
                    return false;
                });

                circle.setOnDeleteListener(view -> {
                    windowManager.removeView(view);
                    circleViews.remove(view);
                });

                windowManager.addView(circle, params);
                circleViews.add(circle);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (controlPanel != null) {
            windowManager.removeView(controlPanel);
        }
        for (DraggableCircleView circle : circleViews) {
            windowManager.removeView(circle);
        }
        circleViews.clear();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public static void requestAndStart(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(context)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else {
                context.startService(new Intent(context, KeySettingFloatService.class));
            }
        } else {
            context.startService(new Intent(context, KeySettingFloatService.class));
        }
    }

    private void checkAndUpdateGlobalEditMode() {
        // 检查是否所有按钮都退出了编辑模式
        boolean anyInEditMode = false;
        for (DraggableCircleView circle : circleViews) {
            if (circle.isInDeleteMode()) {
                anyInEditMode = true;
                break;
            }
        }
        if (!anyInEditMode) {
            globalEditMode = false;
        }
    }

    private void reorderButtonNumbers() {
        // 重新整理按钮序号，从1开始
        for (int i = 0; i < circleViews.size(); i++) {
            DraggableCircleView circle = circleViews.get(i);
            circle.setNumber(i + 1);
        }
        // 更新currentNumber为下一个可用的序号
        currentNumber = circleViews.size() + 1;
        
        // 重新编号后保存位置，确保分组和ID信息得到保持
        savePositions();
    }

    // 更新控制面板位置信息
    private void updateControlPanelPosition() {
        if (controlPanel != null) {
            int[] location = new int[2];
            controlPanel.getLocationOnScreen(location);
            controlPanelLeft = location[0];
            controlPanelTop = location[1];
            controlPanelRight = controlPanelLeft + controlPanel.getWidth();
            controlPanelBottom = controlPanelTop + controlPanel.getHeight();
            android.util.Log.d("KeySettingFloat", "更新控制面板位置: left=" + controlPanelLeft + ", top=" + controlPanelTop + 
                ", right=" + controlPanelRight + ", bottom=" + controlPanelBottom);
        }
    }
} 